package com.sangeng.controller;

import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.entity.LoginUser;
import com.sangeng.domain.entity.User;
import com.sangeng.domain.vo.AdminUserInfoVo;
import com.sangeng.domain.vo.MenuVo;
import com.sangeng.domain.vo.RouterVo;
import com.sangeng.domain.vo.UserInfoVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.exception.SystemException;
import com.sangeng.service.LoginService;
import com.sangeng.service.MenuService;
import com.sangeng.service.RoleService;
import com.sangeng.utils.BeanCopyUtils;
import com.sangeng.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
public class LoginController {
    /**
     改为构造器注入
    @Autowired
    private LoginService loginService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleService roleService;


    因为加入了@RequiredArgsConstructor注解，强制每个final或非空属性必须带一个构造方法，所以不再需要注入。下面的service同理。
    @Autowired
    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }
    */
    private final LoginService loginService;
    private final MenuService menuService;
    private final RoleService roleService;

    @PostMapping("/user/login")
    public ResponseResult<Map<String,String>> login(@RequestBody User user) {
        if (!StringUtils.hasText(user.getUserName())) {
            //提示 必须要传用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return loginService.login(user);
    }

    @PostMapping("/user/logout")
    public ResponseResult<String> logout() {
        //获取当前登录用户id 然后去redis中删除相关的用户信息
        return loginService.logout();
    }

    //后台权限控制及动态路由(所谓的动态路由就是动态根据user的role显示menu)
    @GetMapping("getInfo")
    public ResponseResult<AdminUserInfoVo> getInfo() {
        //获取当前登录用户 同一个线程里面的数据都在SecurityUtils里的SecurityContextHolder里面
        //SecurityContextHolder实际是利用threadlocal
        LoginUser loginUser = SecurityUtils.getLoginUser();
        long id = loginUser.getUser().getId();
        //根据当前用户id查询权限信息
        List<String> perms = menuService.selectPermsByUserId(id);
        //根据当前用户id查询角色信息
        List<String> roleKeyList = roleService.selectRolesByUserId(id);
        //获取用户信息
        User user = loginUser.getUser();
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        //封装vo返回
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perms, roleKeyList, userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }

    @GetMapping("getRouters")
    public ResponseResult<RouterVo> getRouters() {
        Long userId = SecurityUtils.getUserId();
        //查询menu 结果是父子层级菜单的形式
        List<MenuVo> menuVos = menuService.selectRouterMenuTreeByUserId(userId);
        //封装数据返回
        return ResponseResult.okResult(new RouterVo(menuVos));
    }
}